import java.awt.EventQueue;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.*;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;


public class Frame {

	private JFrame frame;
	private JLabel lblIconView;
	private JComboBox typeComboBox;
	private JComboBox iconComboBox;
	private JComboBox objectsComboBox;
	private JTextField typeNameTextField;
	private JTextField iconPathTextField;
	
	private JLabel lblJoinedLabel;
	private ArrayList<Icon> iconsList;
	private ListOfTypes typeList;
	private ListOfObjects objectList;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Frame window = new Frame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Frame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 720, 577);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		typeList = new ListOfTypes(4);
		typeList.addType(new Type("Car"));
		typeList.addType(new Type("Plane"));
		typeList.addType(new Type("Train"));
		typeList.addType(new Type("Truck"));
		
		typeComboBox = new JComboBox();
		
		//add items
		for (int i = 0; i < typeList.getSize(); i++) {
			typeComboBox.addItem(typeList.retrieve(i).getName());
		}
		
		typeComboBox.setBounds(26, 45, 117, 20);
		typeComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int index = typeComboBox.getSelectedIndex();
				Type t = typeList.retrieve(index);				
				Icon iconType = t.getIcon();
				if(iconType == null)
				{
					try {
						lblIconView.setIcon(new ImageIcon());
					}
					catch(NullPointerException ex)
					{
						
					}
					return;
				}
				
				String filename = iconType.getPath();
				ImageIcon icon = new ImageIcon(filename);
				lblIconView.setIcon(icon);
			}
		});
		
		frame.getContentPane().add(typeComboBox);
		
		JLabel lblType = new JLabel("Type");
		lblType.setBounds(26, 26, 46, 14);
		frame.getContentPane().add(lblType);
		
		JLabel lblIcon = new JLabel("Icon");
		lblIcon.setBounds(152, 26, 46, 14);
		frame.getContentPane().add(lblIcon);
		
		iconComboBox = new JComboBox();
		/*iconComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int index = iconComboBox.getSelectedIndex();
				
				if(index == 0)
				{
					//ImageIcon icon = new ImageIcon();
					//lblIconView.setIcon(icon);
					try {
						lblIconView.setIcon(new ImageIcon());
					}
					catch(NullPointerException ex)
					{
						
					}
				}
				else
				{
					Icon iconType = iconsList.get(index-1);
					String filename = iconType.getPath();
					ImageIcon icon = new ImageIcon(filename);
					lblIconView.setIcon(icon);
				}
				
			}
		});*/

		//add icons 
		iconsList = new ArrayList<Icon>();
		iconsList.add(new Icon("Icon 1", "1.jpg"));
		iconsList.add(new Icon("Icon 2", "2.jpg"));
		iconsList.add(new Icon("Icon 3", "3.jpg"));
		iconsList.add(new Icon("Icon 4", "4.jpg"));

		//iconComboBox.setModel(new DefaultComboBoxModel(new String[] {"No Icon", "Icon 1", "Icon 2", "Icon 3", "Icon 4"}));
		iconComboBox.addItem(new String("No Icon"));
		for (Icon icon : iconsList) {
			iconComboBox.addItem(icon.getName());
		}

		iconComboBox.setBounds(152, 45, 117, 20);
		frame.getContentPane().add(iconComboBox);
		
		JButton btnJoin = new JButton("Join Type & Icon");
		btnJoin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				joinType();
			}
		});
		btnJoin.setBounds(26, 114, 225, 32);
		frame.getContentPane().add(btnJoin);
		
		lblJoinedLabel = new JLabel("");
		lblJoinedLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblJoinedLabel.setBounds(26, 513, 501, 14);
		frame.getContentPane().add(lblJoinedLabel);
		
		lblIconView = new JLabel("");
		lblIconView.setBounds(26, 138, 668, 389);
		frame.getContentPane().add(lblIconView);
		
		
		//joinedTypesList = new ArrayList<Type>();
		
		JButton btnAddIcon = new JButton("Add Icon");
		btnAddIcon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addIcon();
			}
		});
		btnAddIcon.setBounds(152, 71, 117, 32);
		frame.getContentPane().add(btnAddIcon);
		
		JButton btnShowIcon = new JButton("Show Icon");
		btnShowIcon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int index = iconComboBox.getSelectedIndex();
				
				if(index == 0)
					return;

				Icon iconType = iconsList.get(index-1);
						        
				ImageIcon imageIcon = new ImageIcon(iconType.getPath());
		        JOptionPane.showMessageDialog(null, "", "View " + iconType.getName(),
		       		 JOptionPane.INFORMATION_MESSAGE, imageIcon);
			}
		});
		btnShowIcon.setBounds(279, 44, 117, 23);
		frame.getContentPane().add(btnShowIcon);
		
		lblJoinedLabel = new JLabel("");
		lblJoinedLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblJoinedLabel.setBounds(26, 513, 501, 14);
		frame.getContentPane().add(lblJoinedLabel);
		
		lblIconView = new JLabel("");
		lblIconView.setBounds(26, 138, 668, 389);
		frame.getContentPane().add(lblIconView);
		
		JLabel lblJoinedType = new JLabel("Objects");
		lblJoinedType.setBounds(548, 26, 146, 14);
		frame.getContentPane().add(lblJoinedType);
		
		
		//objects
		objectList = new ListOfObjects(100);
		objectsComboBox = new JComboBox();
		objectsComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				showObject();
			}
		});
		objectsComboBox.setBounds(548, 45, 146, 20);
		frame.getContentPane().add(objectsComboBox);
		objectsComboBox.addItem("No Objects");

		
		typeNameTextField = new JTextField();
		iconPathTextField = new JTextField();
	}
	
	private void joinType()
	{
		int index = iconComboBox.getSelectedIndex();
		
		if(index == 0)
			return;
		
		Icon iconType = iconsList.get(index-1);
		
		Type type = typeList.retrieve(typeComboBox.getSelectedIndex());
		type.LinkTypeWithIcon(iconType);
        
		ImageIcon imageIcon = new ImageIcon(iconType.getPath());
        JOptionPane.showMessageDialog(null, "We join type '"+type.getName()+"' and icon '"+type.getIcon().getName() +"'", "Join Type",
       		 JOptionPane.INFORMATION_MESSAGE, imageIcon);
        
        addObject();
	}
	
	private void addObject()
	{
		int index = iconComboBox.getSelectedIndex();
		
		if(index == 0)
			return;
		
		Icon iconType = iconsList.get(index-1);
		
		Type type = typeList.retrieve(typeComboBox.getSelectedIndex());
		type.LinkTypeWithIcon(iconType);
		
		Object obj = new Object(iconType.getName() + "_" + type.getName(), type);
		obj.linkObjectWithIcon(iconType);
		
		if(!objectList.addObject(obj))
		{
			return;
		}
		objectsComboBox.addItem(obj.getName());
	}
	
	
	private void showObject()
	{
		int index = objectsComboBox.getSelectedIndex();
		
		if(index <= 0)
			return;
		
		Object object = objectList.retrieve(index-1);
		System.out.println("Object name: " + object.getName());
		
		Icon iconType = object.getIcon();
		String filename = iconType.getPath();
		ImageIcon imageIcon = new ImageIcon(filename);

        JOptionPane.showMessageDialog(null, "OBject '"+object.getName()+"' has icon '"+object.getIcon().getName() +"' and Type '" +object.getT().getName()+ "'", "Object",
        		 JOptionPane.INFORMATION_MESSAGE, imageIcon);/**/
		
	}
	
	private void addIcon()
	{
        int selection = JOptionPane.showConfirmDialog(
                null, getNewIconPanel(), "Add New Icon"
                                , JOptionPane.OK_CANCEL_OPTION
                                , JOptionPane.PLAIN_MESSAGE);

        if (selection == JOptionPane.OK_OPTION) 
        {
        	String text = typeNameTextField.getText();
        	String path = iconPathTextField.getText();
        	if(text.trim().isEmpty() || text.trim().isEmpty())
        		return;
        	
        	iconsList.add(new Icon(text, path));
        	iconComboBox.addItem(text);
        }
	}
	
    private JPanel getNewIconPanel()
    {
        JPanel centerPanel = new JPanel();
        GridBagLayout bagLayout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        centerPanel.setLayout(bagLayout);
        
        JLabel mLabel1 = new JLabel("Icon Name: ");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 0.1;
        c.gridwidth = 1;
        centerPanel.add(mLabel1, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 3;
        centerPanel.add(typeNameTextField, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        centerPanel.add(new JLabel("Icon Path: "), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 2;
        c.weightx = 3;
        centerPanel.add(iconPathTextField, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 3;
        c.gridy = 1;
        c.weightx = 0.1;
        JButton chooseButton = new JButton("+");
        centerPanel.add(chooseButton, c);
        
        chooseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
				FileNameExtensionFilter imageFilter = new FileNameExtensionFilter(
					    "Image files", ImageIO.getReaderFileSuffixes());
				fileChooser.setFileFilter(imageFilter);
				
				int result = fileChooser.showOpenDialog(null);
				if (result == JFileChooser.APPROVE_OPTION) {
				    File selectedFile = fileChooser.getSelectedFile();
				    iconPathTextField.setText(selectedFile.getAbsolutePath());
				}					
			}
		});
        return centerPanel;
    }
}
